$(document).ready(function(){

  var percent=0;
  var points=0;

  $("a[href='#']").click(function() {
    return false
  });



  $('#save1').click(function(){

      var pt1='2,4,6,9';
      var resp='';

      if(!confirm('Seguro de enviar esta respuesta?')){
        return false;
      }

      $('#pt1 input:checked').each(function(){
        resp+=$(this).val()+",";
      });

      resp=resp.substring(0,resp.length-1);

      if(resp===pt1){
        $('#grade1').html('<img src="images/well-done.png"/>');
        percent+=100/8;
        points+=1;
      }else{
        $('#grade1').html('<img src="images/bad-done.png"/>');
      }

      $('#pt1 input').attr('disabled','disabled');
      $('#save1').detach();
  });

  $('#save2').click(function(){
      if(!confirm('Seguro de enviar esta respuesta?')){
        return false;
      }
      var pt2="Singleton"

      var resp=$("input:radio[name ='radiogroup']:checked").val();

      if(resp===pt2){
        $('#grade2').html('<img src="images/well-done.png"/>');
        percent+=100/8;
        points+=1;
      }else{
        $('#grade2').html('<img src="images/bad-done.png"/>');
      }

      $('#pt2 input').attr('disabled','disabled');
      $('#save2').detach();
  });

  $('#save3').click(function(){
      if(!confirm('Seguro de enviar esta respuesta?')){
        return false;
      }
      var pt3="Abstract Factory"

      var resp=$("input:radio[name ='radiogroup2']:checked").val();
      if(resp===pt3){
        $('#grade3').html('<img src="images/well-done.png"/>');
        percent+=100/8;
        points+=1;
      }else{
        $('#grade3').html('<img src="images/bad-done.png"/>');
      }

      $('#pt3 input').attr('disabled','disabled');
      $('#save3').detach();
  });

  $('#save4').click(function(){
      if(!confirm('Seguro de enviar esta respuesta?')){
        return false;
      }
      var pt4="1"

      var resp=$("input:radio[name ='radiogroup3']:checked").val();
      if(resp===pt4){
        $('#grade4').html('<img src="images/well-done.png"/>');
        percent+=100/8;
        points+=1;

      }else{
        $('#grade4').html('<img src="images/bad-done.png"/>');
      }

      $('#pt4 input').attr('disabled','disabled');
      $('#save4').detach();
  });

  $('#save5').click(function(){
      if(!confirm('Seguro de enviar esta respuesta?')){
        return false;
      }
      var pt5="3"

      var resp=$("input:radio[name ='radiogroup4']:checked").val();
      if(resp==pt5){
        $('#grade5').html('<img src="images/well-done.png"/>');
        percent+=100/8;
        points+=1;
      }else{
        $('#grade5').html('<img src="images/bad-done.png"/>');
      }

      $('#pt5 input').attr('disabled','disabled');
      $('#save5').detach();
  });

  $('#save6').click(function(){
      if(!confirm('Seguro de enviar esta respuesta?')){
        return false;
      }

      var respA='';
      var respB='';

      $('#sortable2a li').each(function(){
          respA+=$(this).html()+",";
      });

      $('#sortable2b li').each(function(){
          respB+=$(this).html()+",";
      });

      if(respA==="Singleton,Factory Method," || respA==="Factory Method,Singleton,"){
          if(respB==="Builder,Abstract Factory," || respB==="Abstract Factory,Builder,"){
              $('#grade6').html('<img src="images/well-done.png"/>');
              percent+=100/8;
              points+=1;
          }else{
              $('#grade6').html('<img src="images/bad-done.png"/>');
          }
      }else{
          $('#grade6').html('<img src="images/bad-done.png"/>');
      }
      $('ul, li').removeClass('ui-sortable');
      $('ul, li').removeClass('ui-draggable');
      $('ul, li').removeClass('ui-droppable');

      $('#save6').detach();

  });

  $('#save7').click(function(){
      if(!confirm('Seguro de enviar esta respuesta?')){
        return false;
      }
      var pt7="1"

      var resp=$("input:radio[name ='radiogroup5']:checked").val();
      if(resp==pt7){
        $('#grade7').html('<img src="images/well-done.png"/>');
        percent+=100/8;
        points+=1;
      }else{
        $('#grade7').html('<img src="images/bad-done.png"/>');
      }

      $('#pt7 input').attr('disabled','disabled');
      $('#save7').detach();
  });

  $('#save8').click(function(){
      if(!confirm('Seguro de enviar esta respuesta?')){
        return false;
      }
      var pt8="3,4,2,4"
      var resp='';

      $('form#pt8a select option:selected').each(function(){
          resp+=$(this).val()+",";
      });

      $('form#pt8b select option:selected').each(function(){
          resp+=$(this).val()+",";
      });

      resp=resp.substring(0,resp.length-1);

      if(resp===pt8){
        $('#grade8').html('<img src="images/well-done.png"/>');
        percent+=100/8;
        points+=1;
      }else{
        $('#grade8').html('<img src="images/bad-done.png"/>');
      }

      $('#pt8a select, #pt8b select').attr('disabled','disabled');
      $('#save8').detach();
      $('article .btn').detach();
      $('.end').css('display','');
  });

  $('.end').click(function(){
      percent=(points/8)*100
      $('#success').html(points+' ('+percent+'%)');
      $('#failed').html((8-points)+' ('+(100-percent)+'%)');
      $('#final').html((5*percent/100).toFixed(1));
  });

  $('.sortable').sortable().disableSelection();
  $('.sortable li').draggable({helper:'clone'}).disableSelection();
  $('.dropped li').draggable({helper:'clone'}).disableSelection();

  $(".trash_list ").droppable({
    accept: ".sortable li",
    drop: function(ev, ui) {
                    // Añado el objeto origen a la lista destino
                    $(this).append($(ui.draggable));
                  }
                });

  $(".sortable").droppable({
    accept: "li",
    drop: function(ev, ui) {
                    // Añado el objeto origen a la lista destino
                    $(this).append($(ui.draggable));
                  }
                });



});